package com.yb.web.shoper;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.global.SystemLogService;
import com.yb.bussiness.global.SystemLogServiceImpl;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.bussiness.goods.TpingGouService;
import com.yb.bussiness.goods.TpingGouServiceImpl;
import com.yb.bussiness.shoper.BrandService;
import com.yb.bussiness.shoper.BrandServiceImpl;
import com.yb.domain.goods.TgBrand;
import com.yb.domain.goods.TgCategory;
import com.yb.domain.goods.TpingGou;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.MemoryData;

public class TpPingGouAction extends BaseAction {
	private TpingGouService service;
	private CategoryService categoryService;
	private BrandService brandService;
	private List rows;
	private int page;
	private int total;
	private List<TgCategory> catrows;
	private Long categoryId;
	private Long typeId;
	private String[] brandId;
	private SystemLogService systemLogService;
	
	
	public TpPingGouAction() {
		service = new TpingGouServiceImpl();
		categoryService = new CategoryServiceImpl();
		brandService = new BrandServiceImpl();
		systemLogService = new SystemLogServiceImpl();
	}

	/**
	 * 显示拼购信息列表出错
	 * 
	 * @return
	 */
	public String showList() {
		try {
			catrows = categoryService.findAll(1, 50);
		} catch (Exception e) {
			log.error("显示拼购信息列表出错", e);
		}
		return "showList";
	}

	public String search() {
		try {
			Long categoryId = RequestUtil.getParam(request, "categoryId", 0l);
			Long bigCategory = RequestUtil.getParam(request, "bigCategory", 0l);
			Long typeId = RequestUtil.getParam(request, "typeId", 0l);
			String name = RequestUtil.getParam(request, "name", "");
			int limit = RequestUtil.getParam(request, "rows", 20);
			Map<String, Object> map = brandService.findBrandByTypeAndCategroy(
					categoryId, typeId,bigCategory, page, limit,name);
			rows = (List) map.get("data");
			total = new Integer(map.get("count").toString());
		} catch (Exception e) {
			log.error("查询拼购信息出错", e);
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	/**
	 * 跳转到拼购页面
	 */
	public String goForwordPage() {
		try {
			rows = categoryService.findAll(1, 50);
		} catch (Exception e) {
			log.error("显示品牌产品分类错误", e);
		}
		return "page";
	}

	public String savePinGou() {
		try {
			if (null != brandId) {
				for (int i = 0; i < brandId.length; i++) {
					String bid = brandId[i];
					Long brandPk = new Long(bid);
					TpingGou tpg = new TpingGou();
					tpg.setBrandId(brandPk);
					tpg.setCategoryId(categoryId);
					if (null != typeId && typeId != 0l) {
						tpg.setTypeId(typeId);
					}
					service.addOrUpdate(tpg);
				}
			}
		} catch (Exception e) {
		}
		return "saveSuccess";
	}

	public String deletePingGou() {
		try {
			String id = RequestUtil.getParam(request, "deleteId", "");
			if (service.checkDel(id, categoryId, typeId)) {
				String[] idAry = id.split("_");
				for (int i = 0; i < idAry.length; i++) {
					systemLogService.saveSystemLog(request, "拼购设置", "删除拼购信息--"+brandService.findById(idAry[i]).getName());
					service.delete(new Long(idAry[i]), categoryId, typeId);
				}
				response.getWriter().print("1");
			} else {
				response.getWriter().print("3");
			}
		} catch (Exception e) {
			log.error("批量删除拼购信息错误", e);
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String modifyWeights() {
		try {
			Long brandId = RequestUtil.getParam(request, "brandId", 0l);
			Long categoryId = RequestUtil.getParam(request, "categoryId", 0l);
			Long typeId = RequestUtil.getParam(request, "typeId", -1l);
			int weights = RequestUtil.getParam(request, "weights", 0);
			service.modifyWeights(brandId, categoryId, typeId, weights);
			systemLogService.saveSystemLog(request, "拼购设置", "修改权重--"+brandService.findById(brandId.toString()).getName());
			response.getWriter().print("1");
		} catch (Exception e) {
			log.error("批量删除拼购信息错误", e);
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	public String pingouIndex() {
		try {
			List<TgCategory> list = MemoryData.getCategoryList();
			Map<Long, Object> map = new HashMap<Long, Object>();
			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				TgCategory category = (TgCategory) iterator.next();
				List<TgBrand> brandList = service
						.findPingouByCategoryId(category.getId());
				map.put(category.getId(), brandList);
			}
			ActionContext.getContext().put("categoryList", list);
			ActionContext.getContext().put("map", map);
		} catch (Exception e) {
			log.error("拼购首页推荐", e);
		}
		return "pingouIndex";
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public Long getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(Long categoryId) {
		this.categoryId = categoryId;
	}

	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String[] getBrandId() {
		return brandId;
	}

	public void setBrandId(String[] brandId) {
		this.brandId = brandId;
	}

	public List<TgCategory> getCatrows() {
		return catrows;
	}

	public void setCatrows(List<TgCategory> catrows) {
		this.catrows = catrows;
	}

}
